package org.epidroid.epidroid.epirss;

import org.epidroid.epidroid.EpiDroid;
import org.epidroid.epidroid.R;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.TextView;

public class RssArticleView extends Activity {
	String url;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rss_article);

		Bundle b = getIntent().getExtras();
		int rowid = b.getInt("articleId");

		/* Retrieve info. */
		Cursor c = EpiDroid.rssDb.fetchArticle(rowid);
		String title = c.getString(c.getColumnIndex("title"));
		//String body = c.getString(c.getColumnIndex("desc"));
		String htmlText = c.getString(c.getColumnIndex("content"));
		//String image = "";
		this.url = c.getString(c.getColumnIndex("url"));

		/* Image url. */
		//Matcher mat = Pattern.compile("\\[\\[.*\\]\\]").matcher(body);
		//while (mat.find())
		//{
		//	body = body.replace(mat.group(), "");
		//	String name = mat.group().replace("[[", "").replace("]]", "");
		//	image = "http://blogs.ionis-group.com/epita/media/" + name;
		//	htmlText = "<div style='text-align: center'><img src='" + image + "' alt='image' /></div>" + htmlText;
		//}
		
		htmlText = htmlText.replace("%", "&#37;");

		/** Fill view. */
		//((TextView) findViewById(R.id.articleTitle)).setText("");
		((TextView) findViewById(R.id.rss_article)).setText(title);
		//((TextView) findViewById(R.id.articleBody)).setText(body);
		((WebView) findViewById(R.id.htmlText)).loadDataWithBaseURL("", htmlText, "text/html", "UTF-8", "");

		/* Free cursor. */
		c.close();

		/** Handle button "read article". */
		//Button rssBtn = (Button) findViewById(R.id.go_article);
		//rssBtn.setOnClickListener(readArticleListener);
	}

	// Create an anonymous implementation of OnClickListener
	private OnClickListener readArticleListener = new OnClickListener() {
		public void onClick(View v) {
			Intent browserIntent = new Intent(
					"android.intent.action.VIEW",
					Uri.parse(url)
			);
			startActivity(browserIntent);
		}
	};
}
